/**
 * Copyright (C) 2012 Oleg Anastassov <o.anastasov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.ent.desktop.run;

import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import kz.kstu.ent.app.context.AppContext;
import kz.kstu.ent.app.delegate.api.AccountDelegate;
import kz.kstu.ent.app.delegate.impl.AccountDelegateImpl;
import kz.kstu.ent.business.entity.security.Account;
import kz.kstu.ent.desktop.ui.dialog.security.LoginDialog;
import kz.kstu.ent.desktop.ui.frame.ApplicationFrame;
import kz.kstu.ent.desktop.util.GUIUtils;
import kz.kstu.ent.desktop.util.health.HealthManager;
import kz.kstu.ent.desktop.util.shell.SplashScreen;
import org.apache.log4j.Logger;

import javax.swing.*;
import java.util.UUID;

/**
 * @author Oleg Anastassov
 */
public class ENTClient {

    private static final Logger LOGGER = Logger.getLogger(ENTClient.class);
    private UUID sessionUUID;
    private static ApplicationFrame applicationFrame;

    public ENTClient() {
    }

    public static void main(String[] args) {
        ENTClient client = new ENTClient();
        client.run();
    }

    public void run() {
        LookAndFeel lookAndFeel = new NimbusLookAndFeel();
        putUIMessages();
        try {
            UIManager.setLookAndFeel(lookAndFeel);
        } catch (UnsupportedLookAndFeelException e) {
            LOGGER.error(e.getMessage(), e);
        }

        SplashScreen.getInstance().show();

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                sessionUUID = UUID.randomUUID();
                LOGGER.info("sessionUID='" + sessionUUID + "' starts");
                AccountDelegate delegate = new AccountDelegateImpl();
                LOGGER.info("Total user count = " + delegate.getUserCount());
                SplashScreen.getInstance().hide();
                applicationFrame = new ApplicationFrame();
                LoginDialog loginDialog = new LoginDialog();
                LOGGER.info("Try to login...");
                Account account = loginDialog.login();
                if (account != null) {
                    LOGGER.info("Logged as '" + account.getLogin() + "'");
                    AppContext.getInstance().setAccount(account);
                    applicationFrame.initStaticComponents();
                    // applicationFrame.initData();
                    GUIUtils.centerAndShow(applicationFrame);
                    LOGGER.info("sessionUID='" + sessionUUID + "' ends");
                } else {
                    LOGGER.error("Access denied!");
                    applicationFrame.close();
                }
            }
        };
        HealthManager.getInstance().logAnyError();
        SwingUtilities.invokeLater(runnable);
    }

    public static JFrame getMainFrame() {
        return applicationFrame;
    }

    private void putUIMessages() {
        // костыльная русификация диалогов
        UIManager.put("OptionPane.cancelButtonMnemonic", "0");
        UIManager.put("OptionPane.cancelButtonText", "Отмена");
        UIManager.put("OptionPane.inputDialogTitle", "Ввод");
        UIManager.put("OptionPane.messageDialogTitle", "Сообщение");
        UIManager.put("OptionPane.noButtonMnemonic", "78");
        UIManager.put("OptionPane.noButtonText", "Нет");
        UIManager.put("OptionPane.okButtonMnemonic", "0");
        UIManager.put("OptionPane.okButtonText", "OK");
        UIManager.put("OptionPane.titleText", "Выберите значение");
        UIManager.put("OptionPane.yesButtonMnemonic", "89");
        UIManager.put("OptionPane.yesButtonText", "Да");
    }
}

